package net.huashitong.zhApp.controller;

import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.filter.NewProjectsFilter;
import net.huashitong.Project.responseModel.MessageModel;
import net.huashitong.Project.service.MyProjectsService;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.oversee.databean.ProjectScoreRankDataBean;
import net.huashitong.oversee.service.GradePointService;
import net.huashitong.oversee.service.ScoreSummaryService;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.service.BnpUserService;
import net.huashitong.user.service.MessageService;
import net.huashitong.zhApp.dataBean.OperationLogEffect;
import net.huashitong.zhApp.service.AppGradePointService;
import net.huashitong.zhApp.service.AppProjectsService;
import net.huashitong.zhApp.service.OperationLogEffectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime2;

@Controller
@RequestMapping(value = "/zhApp/leader")
public class AppLeaderController extends BaseCtrl {

    @Lazy
    @Autowired
    private AppProjectsService appProjectsService;
    @Lazy
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private MyProjectsService myProjectsService;
    @Autowired
    private AppGradePointService appGradePointService;
    @Autowired
    private BnpUserService userService;
    @Autowired
    private OperationLogEffectService operationLogEffectService;
    @Lazy
    @Autowired
    private MessageService messageService;

    /**
     * 查询项目汇总（领导首页已经开工未开工分类）
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryProjectSummary", method = RequestMethod.POST)
    public Map<String, Object> queryProjectSummary(@RequestBody NewProjectsFilter filter) {
        BnpUser user = queryUserAuth(filter.getUserPhone());
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        List<NewProjects> resList = appProjectsService.queryProjectsNoPageByUser(user, filter);
        Map<String, Integer> resProMap = new TreeMap<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        if ("1".equals(filter.getTypeFlag())) {
            //按县处级领导分类
            for (NewProjects project : resList) {
                if (!resProMap.containsKey(project.getResUser())) {
                    resProMap.put(project.getResUser(), 1);
                } else {
                    Integer val = resProMap.get(project.getResUser());
                    resProMap.put(project.getResUser(), val + 1);
                }
            }
        }
        if ("2".equals(filter.getTypeFlag())) {
            //按责任单位分类
            for (NewProjects project : resList) {
                if (!resProMap.containsKey(project.getResDept())) {
                    resProMap.put(project.getResDept(), 1);
                } else {
                    Integer val = resProMap.get(project.getResDept());
                    resProMap.put(project.getResDept(), val + 1);
                }
            }
        }
        for (Map.Entry<String, Integer> entry : resProMap.entrySet()) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", entry.getKey());
            map.put("count", entry.getValue());
            resultList.add(map);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("userAuth", user);
        result.put("data", resultList);
        return result;
    }

    /**
     * 领导页面统计项目详情（APP端）
     *
     * @param filter
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping(value = "/queryProjectDetailByProjectId", method = RequestMethod.POST)
    public Map<String, Object> queryProjectDetailByProjectId(@RequestBody NewProjectsFilter filter) throws ParseException {

        Map<String, Object> result = new HashMap<>();
        NewProjects project = newProjectsService.getById(filter.getProjectId());
        Map<String, Object> resMap = appProjectsService.queryProjectDetailByProject(project);
        if (!resMap.containsKey("data1")) {
            result.put("code", "200");
            result.put("message", "成功获取数据");
            result.put("data", resMap);
            return result;
        } else {
            Map<String, Object> data = new HashMap<>();
            Map<String, Object> data1 = new HashMap<>();
            for (Map.Entry<String, Object> entry : resMap.entrySet()) {
                if (!"data1".equals(entry.getKey())) {
                    data.put(entry.getKey(), entry.getValue());
                } else {
//                    result.put("data1", entry.getValue());
                    data1.put(entry.getKey(), entry.getValue());
                }
            }
            result.put("code", "200");
            result.put("message", "成功获取数据");
            result.put("data", data);
            result.put("data1", data1);
            return result;
        }
    }


    /**
     * 项目概览（app领导页面）
     *
     * @param phone
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getProjectOverview/{phone}", method = RequestMethod.GET)
    public Map<String, Object> getProjectOverview(@PathVariable String phone) {
        BnpUser user = queryUserAuth(phone);
        if (ObjectUtils.isBlank(user)) {
            return coverMessage("202", "无用户权限");
        }
        Map<String, Object> data = myProjectsService.queryStatisticsSummary(user);
        Map<String, String> contentMap = myProjectsService.generateOverview(data);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("userAuth", user);
        result.put("data", contentMap);
        return result;
    }


    /**
     * 县处级领导绩效考核页面，查看自己分管的项目得分
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryScoreSummaryInRes", method = RequestMethod.POST)
    public Map<String, Object> getProjectOverview(@RequestBody NewProjectsFilter filter) {
        if (StringUtils.isNotBlank(filter.getFlag())) {
            if ("5".equals(filter.getFlag())) {
//                BnpUser user = queryUserAuth(filter.getFlag());
//                ScoreSummaryFilter scoreSummaryFilter = new ScoreSummaryFilter();
//                scoreSummaryFilter.setBranchType("5");
//                scoreSummaryFilter.setUserId(user.getSysId());
//                scoreSummaryService.queryByFilterNoPage()
//                GradePointFilter gf = new GradePointFilter();
//                gf.setCheckType("攻坚任务");
//                gf.setCheckObjectId(user.getSysId());
//                List<GradePoint> gradePoints = gradePointService.queryGradePoint(gf);
//                Map<>
//                for (GradePoint gradePoint : gradePoints) {
//                }
            }
        }

        List<ProjectScoreRankDataBean> data = appGradePointService.statisticalItemScore(filter.getResPhone(), filter.getStatus());
        DecimalFormat df = new DecimalFormat("######0.00");
        for (ProjectScoreRankDataBean d : data) {
            Double str = d.getFinalScore();
            String val = df.format(str);
            double doubleVal = Double.parseDouble(val);
            d.setFinalScore(doubleVal);
            Double score = d.getScore();
            double scoreVal = Math.round(score * 100) / 100;
            d.setScore(scoreVal);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", data);
        return result;
    }

    /**
     * 县处级领导绩效考核页面，查看自己分管的项目得分
     *
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryProjectScoreInRes", method = RequestMethod.POST)
    public Map<String, Object> queryProjectScoreInRes(@RequestBody NewProjectsFilter filter) {
        String userPhone = filter.getUserPhone();
        String checkObject = "";
        switch (userPhone) {
            case "13005912001":
                checkObject = "许以章";
                break;
            case "13005912002":
                checkObject = "姚伟";
                break;
            case "13005912003":
                checkObject = "柯永华";
                break;
            case "13005912004":
                checkObject = "王礼灯";
                break;
            case "13005912005":
                checkObject = "吴钢";
                break;
            case "13005912006":
                checkObject = "郑建双";
                break;
            case "13005912007":
                checkObject = "曹方敏";
                break;
            case "13005912008":
                checkObject = "李卫榕";
                break;
            case "13005912009":
                checkObject = "陈榕";
                break;
            case "13005912010":
                checkObject = "张庆宗";
                break;
            case "13005912011":
                checkObject = "黄身瑜";
                break;
            case "13005912012":
                checkObject = "罗智林";
                break;
            case "13005912013":
                checkObject = "俞华";
                break;
            case "13005912014":
                checkObject = "魏秀惠";
                break;
            case "13005912015":
                checkObject = "侯一晞";
                break;
            case "13005911002":
                checkObject = "唐磊";
                break;
            case "13005912017":
                checkObject = "余斌";
                break;
            case "13005912018":
                checkObject = "姜应明";
                break;
            case "13005912019":
                checkObject = "邱小文";
                break;
            case "13005912020":
                checkObject = "丁虹";
                break;
            case "13005912021":
                checkObject = "叶俊忠";
                break;
            case "13005912022":
                checkObject = "张文";
                break;
            case "13005912023":
                checkObject = "李志专";
                break;
            case "13005912024":
                checkObject = "江惠文";
                break;
            case "13005912025":
                checkObject = "张培奋";
                break;
            case "13005912026":
                checkObject = "史瑜";
                break;
            case "13005912027":
                checkObject = "王宁";
                break;
            case "13005912028":
                checkObject = "林孝仕";
                break;
            case "13005912029":
                checkObject = "上官浩龙";
                break;
            default:
                break;
        }
        BnpUserFilter userFilter = new BnpUserFilter();
        userFilter.setName(checkObject);
        List<BnpUser> users = userService.queryByFilter(userFilter);
        if (ObjectUtils.isNotBlank(users)) {
            filter.setResPhone(users.get(0).getPhone());
        } else {
            filter.setResPhone("");
        }
        List<ProjectScoreRankDataBean> data = appGradePointService.statisticalItemScore(filter.getResPhone(), null);
        DecimalFormat df = new DecimalFormat("######0.00");
        for (ProjectScoreRankDataBean d : data) {
            Double str = d.getFinalScore();
            String val = df.format(str);
            double doubleVal = Double.parseDouble(val);
            d.setFinalScore(doubleVal);
            d.setTackTaskScore(0.0);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", data);
        return result;
    }


    //运行日志成效
    @ResponseBody
    @RequestMapping(value = "/queryOperationLogEffect", method = RequestMethod.GET)
    public Map<String, Object> queryOperationLogEffect() {
        List<OperationLogEffect> list = new ArrayList<>();
        //运行成效（一周时间生成几条督查任务）
        int num1 = operationLogEffectService.queryTaskNumInWeek();
        //运行成效（一周时间完成几条节点任务）
        int num2 = operationLogEffectService.queryFinishNodesNumInWeek();
        //运行成效（一周时间发送几条短信记录）
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //过去七天
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE, -7);
        Date d = c.getTime();
        String day = format.format(d);

        List<MessageModel> messageList = messageService.getMessageByDate(day, getCurrentDateTime2());
        int num3 = messageList.size();
        OperationLogEffect v1 = new OperationLogEffect(
                "ZD",
                "重大系统总共生成" + num1 + "条督查任务。",
                "生成督查任务数",
                "重大项目管理系统");
        list.add(v1);
        OperationLogEffect v2 = new OperationLogEffect(
                "ZD",
                "重大系统总共完成" + num2 + "条节点任务。",
                "完成节点任务数",
                "重大项目管理系统");
        list.add(v2);
        OperationLogEffect v3 = new OperationLogEffect(
                "ZD",
                "重大系统总共发送" + num3 + "条消息提醒。",
                "发送消息提醒数",
                "重大项目管理系统");
        list.add(v3);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", list);
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/queryProjectDetail", method = RequestMethod.POST)
    public Map<String, Object> queryProjectDetail(@RequestBody NewProjectsFilter filter) {
        String userPhone = filter.getUserPhone();
        String checkObject = "";
        switch (userPhone) {
            case "13005912001":
                checkObject = "许以章";
                break;
            case "13005912002":
                checkObject = "姚伟";
                break;
            case "13005912003":
                checkObject = "柯永华";
                break;
            case "13005912004":
                checkObject = "王礼灯";
                break;
            case "13005912005":
                checkObject = "吴钢";
                break;
            case "13005912006":
                checkObject = "郑建双";
                break;
            case "13005912007":
                checkObject = "曹方敏";
                break;
            case "13005912008":
                checkObject = "李卫榕";
                break;
            case "13005912009":
                checkObject = "陈榕";
                break;
            case "13005912010":
                checkObject = "张庆宗";
                break;
            case "13005912011":
                checkObject = "黄身瑜";
                break;
            case "13005912012":
                checkObject = "罗智林";
                break;
            case "13005912013":
                checkObject = "俞华";
                break;
            case "13005912014":
                checkObject = "魏秀惠";
                break;
            case "13005912015":
                checkObject = "侯一晞";
                break;
            case "13005911002":
                checkObject = "唐磊";
                break;
            case "13005912017":
                checkObject = "余斌";
                break;
            case "13005912018":
                checkObject = "姜应明";
                break;
            case "13005912019":
                checkObject = "邱小文";
                break;
            case "13005912020":
                checkObject = "丁虹";
                break;
            case "13005912021":
                checkObject = "叶俊忠";
                break;
            case "13005912022":
                checkObject = "张文";
                break;
            case "13005912023":
                checkObject = "李志专";
                break;
            case "13005912024":
                checkObject = "江惠文";
                break;
            case "13005912025":
                checkObject = "张培奋";
                break;
            case "13005912026":
                checkObject = "史瑜";
                break;
            case "13005912027":
                checkObject = "王宁";
                break;
            case "13005912028":
                checkObject = "林孝仕";
                break;
            case "13005912029":
                checkObject = "上官浩龙";
                break;
            default:
                break;
        }
        BnpUserFilter userFilter = new BnpUserFilter();
        userFilter.setName(checkObject);
        List<BnpUser> users = userService.queryByFilter(userFilter);
        if (users.size() == 1) {
            filter.setResPhone(users.get(0).getPhone());
        }
        List<NewProjects> data = newProjectsService.queryByfilter(filter);
        Map<String, Object> result = new HashMap<>();
        result.put("code", "200");
        result.put("message", "成功获取数据");
        result.put("data", data);
        return result;
    }
}
